package xwbank.jmeter5.scripts.rabbitmq;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

public class RabbitMQTest extends AbstractJavaSamplerClient {
    private static ApplicationContext context = null;
    private RabbitTemplate template = null;

    @Override
    public Arguments getDefaultParameters() {
        Arguments arguments = new Arguments();
        arguments.addArgument("exchange","");
        arguments.addArgument("routingKey","");
        arguments.addArgument("message","");
        return arguments;
    }

    @Override
    public void setupTest(JavaSamplerContext arg0) {
        context = new ClassPathXmlApplicationContext(new String[] {"env/serviceperf/rabbitmq-cmp-host.xml"});
        template = context.getBean(RabbitTemplate.class);
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        String exchange = javaSamplerContext.getParameter("exchange");
        String routingKey = javaSamplerContext.getParameter("routingKey");
        String message = javaSamplerContext.getParameter("message");

        SampleResult result = new SampleResult();
        result.sampleStart();

        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        try{
            MessageProperties messageProperties = new MessageProperties();
            messageProperties.setContentType("application/json");
            messageProperties.setContentEncoding("UTF-8");
            Message msg = new Message(message.getBytes("UTF-8"),messageProperties);
            template.convertAndSend(exchange,routingKey,msg,correlationId);
            result.setResponseHeaders("response: " + "消息发送成功");
            result.setRequestHeaders("request: " + message);
            result.setDataType(SampleResult.TEXT);
            result.setSuccessful(true);
        } catch (UnsupportedEncodingException e) {
            result.setResponseHeaders("response: " + "消息发送失败");
            result.setRequestHeaders("request: " + message);
            result.setDataType(SampleResult.TEXT);
            result.setSuccessful(false);
            e.printStackTrace();
        }finally {
            result.sampleEnd();
        }
        return result;
    }

    @Override
    public void teardownTest(JavaSamplerContext context) {
        super.teardownTest(context);
    }

    public static void main(String[] args) {
        Arguments arguments = new Arguments();
        arguments.addArgument("exchange","loanInitQueue");
        arguments.addArgument("routingKey","q.cmp-sch.loanInitQueue");
        arguments.addArgument("message","{\"ni hao\":\"good\"}");
        JavaSamplerContext context = new JavaSamplerContext(arguments);
        RabbitMQTest test = new RabbitMQTest();
        test.setupTest(context);
        test.runTest(context);
        test.teardownTest(context);
    }
}
